package com.cg.asm.body.service.menu.impl;

import com.cg.asm.body.dao.menu.MenuMapper;
import com.cg.asm.body.domain.dto.menu.FunListRegDTO;
import com.cg.asm.body.domain.dto.menu.FunPointsDTO;
import com.cg.asm.body.domain.vo.menu.MenuListVO;
import com.cg.asm.body.domain.vo.menu.MenuPointsPagedVO;
import com.cg.asm.body.service.menu.IMenuService;
import com.cg.asm.body.util.AsmBodyCommonUtil;
import com.cg.asm.client.AsmUUID;
import com.cg.asm.client.exception.AsmException;
import com.cg.asm.client.exception.MybatisExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MenuService implements IMenuService {

    private MenuMapper menuMapper;

    public MenuService(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    /**
     * 获取功能清单树信息
     * 使用场景：
     * 1.功能清单主页面
     *
     * @return
     */
    @Override
    public List<MenuListVO> getMenuTreeData() {
        return menuMapper.getMenuTreeData();
    }

    /**
     * 保存顶级菜单信息
     *
     * @param funListRegDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMenuRoot(FunListRegDTO funListRegDTO) {
        if (null != funListRegDTO && StringUtil.isNotEmpty(funListRegDTO.getFunListName())) {
            funListRegDTO.setFunListCode(AsmUUID.generate());
            funListRegDTO.setCreatorUserId(AsmBodyCommonUtil.getCurrentUserLoginVO().getUserId());
            funListRegDTO.setCreatorDate(new Date());
            funListRegDTO.setSelfLevelCode(AsmUUID.generate());
            menuMapper.saveMenuRoot(funListRegDTO);
        } else {
            throw new AsmException("保存菜单信息失败，未获取到需增加的信息，或功能清单为空！");
        }
    }

    /**
     * 删除功能清单
     *
     * @param funListCode
     * @return
     */
    @Override
    public void delMenu(String funListCode) {
        if (StringUtil.isNotEmpty(funListCode)) {
            menuMapper.delMenu(funListCode);
            menuMapper.delPointsByFunListCode(funListCode);
            this.delRoleMenu(funListCode);
        } else {
            throw new AsmException("删除失败，功能清单编码不能为空!");
        }
    }

    /**
     * 根据菜单编码，查询菜单详细信息
     *
     * @param funListCode
     * @return
     */
    @Override
    public MenuListVO getMenuByCode(String funListCode) {
        MenuListVO menuListVO = menuMapper.getMenuByCode(funListCode);
        if (null != menuListVO && StringUtil.isNotEmpty(menuListVO.getFunListOrder())) {
            String editFunListOrder = menuListVO.getFunListOrder();
            int lastIndex = editFunListOrder.lastIndexOf("-");
            if (lastIndex != -1) {
                menuListVO.setParentFunListOrder(editFunListOrder.substring(0, lastIndex + 1));
                menuListVO.setOrder(editFunListOrder.substring(lastIndex + 1));
            } else {
                menuListVO.setOrder(editFunListOrder);
            }
        }
        return menuListVO;
    }

    /**
     * 保存编辑菜单信息
     *
     * @param funListRegDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEditMenu(FunListRegDTO funListRegDTO) {
        if (null != funListRegDTO && StringUtil.isNotEmpty(funListRegDTO.getFunListCode())) {
            String funListCode = funListRegDTO.getFunListCode();
            menuMapper.delMenu(funListCode);
            menuMapper.saveMenuRoot(funListRegDTO);
        } else {
            throw new AsmException("保存编辑菜单信息失败，未获取到编码信息！");
        }
    }

    /**
     * 获取功能清单功能点列表信息
     *
     * @param mybatisExample
     * @return
     */
    @Override
    public PageInfo<MenuPointsPagedVO> funPointsPagedList(MybatisExample mybatisExample) {
        PageHelper.startPage(mybatisExample.getOffset(), mybatisExample.getLimit());
        List<MenuPointsPagedVO> roles = menuMapper.funPointsPagedList(mybatisExample);
        return new PageInfo<>(roles);
    }

    /**
     * 验证功能点编码是否唯一
     *
     * @param funPointsCode
     * @return false: 不是唯一
     * true: 是唯一
     */
    @Override
    public boolean funPointsCodeIsUnique(String funPointsCode) {
        String resultCode = menuMapper.funPointsCodeIsUnique(funPointsCode);
        if (StringUtil.isNotEmpty(resultCode)) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void saveFunPoints(FunPointsDTO funPointsDTO) {
        menuMapper.saveFunPoints(funPointsDTO);
    }

    @Override
    public void delPoints(String funPointsCode) {
        menuMapper.delPoints(funPointsCode);
    }

    @Override
    public void saveEditPoints(FunPointsDTO funPointsDTO) {
        if (null != funPointsDTO && StringUtil.isNotEmpty(funPointsDTO.getFunPointsCode())) {
            menuMapper.saveEditPoints(funPointsDTO);
        } else {
            throw new AsmException("更新失败，未获取到要更新的信息，或功能点编码为空！");
        }
    }

    /**
     * 删除角色与菜单关联信息
     *
     * @param funListCode
     */
    private void delRoleMenu(String funListCode) {
        menuMapper.delRoleMenu(funListCode);
        menuMapper.delRoleFunPoints(funListCode);
    }

}
